package com.export.resolve;

import com.export.utils.ExcelUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ResolveMark {
    /**
     * 匹配 ${}字符
     */
    private final static String PATTERN1 = "\\$\\{\\S+\\}";

    /**
     * 匹配${ss}中的内容ss
     */
    private final static String PATTERN2 = "(?<=\\$\\{)(\\S+)(?=\\})";

    /**
     * 匹配<%= %>字符
     */
    private final static String PATTERN3 = "^<%=\\S+%>$";
    /**
     * 匹配<%=SUM(D1:D3)%>字符中的内容
     */
    private final static String PATTERN4 = "(?<=^<%=)(\\S+)(?=%>)";

    /**
     * 匹配
     *
     * @param cell
     * @param data
     * @return
     */
    public static Cell replaceMark(Cell cell,Cell need2InitCell, Object data) throws Exception{
            String value = ExcelUtils.getCellvalue(cell);
            Pattern pa1 = Pattern.compile(PATTERN1);
            Pattern pa3 = Pattern.compile(PATTERN3);
            Matcher matcher1 = pa1.matcher(value);
            Matcher matcher3 = pa3.matcher(value);
            if (matcher1.find()) {
                //解析${}标签
                String temp = replaceValue(value,data);
                if(isNumeric(temp)){
                    need2InitCell.setCellType(CellType.NUMERIC);
                    need2InitCell.setCellValue(Double.valueOf(temp));
                }else{
                    need2InitCell.setCellType(CellType.STRING);
                    need2InitCell.setCellValue(temp);
                }
            } else if (matcher3.find()) {
                //解析到<%= %>标签
                String temp = replaceFormula(value);
                need2InitCell.setCellFormula(temp);
            } else {
                ExcelUtils.putValue(need2InitCell,cell.getCellType(),value);
            }
            return  need2InitCell;
    }

    /**
     * 替换${}中的内容
     *
     * @param value
     * @param data
     * @return
     */
    private static String replaceValue(String value, Object data) throws Exception {
        Pattern pa2 = Pattern.compile(PATTERN2);
        Matcher matcher = pa2.matcher(value);
        if (matcher.find()) {
            String prop = matcher.group();
            String res = BeanUtils.getProperty(data, prop).toString();
            return res;
        }else{
            throw new RuntimeException("未找到${}里的内容");
        }
    }

    /**
     * 替换<%= %>中的内容
     *
     * @param value
     * @return
     */
    private static String replaceFormula(String value) {
        Pattern pa4 = Pattern.compile(PATTERN4);
        Matcher matcher = pa4.matcher(value);
        if (matcher.find()) {
            String prop = matcher.group();
            return prop;
        }else{
            throw new RuntimeException("未找到<%= %>里的内容");
        }
    }

    /**
     * 判断是否是数字
     *
     * @param s
     * @return
     */
    private static boolean isNumeric(String s) {
        if (s != null && !"".equals(s.trim()))
            return s.matches("^[0-9]*$");
        else
            return false;
    }

}
